﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using rbac.api.Filters;
using rbac.contracts.Dto.Output;
using rbac.contracts.Interface;
using rbac.domain.Impl;

namespace rbac.api.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 角色查询
        /// </summary>
        /// <param name="roleNaem">角色名称</param>
        /// <returns></returns>
        [HttpGet]
        [RbacAuthorize("AdminAndCustomer")]
        public Results<List<RoleOutput>> SearchRoles(string? roleNaem)
        {
            return Results<List<RoleOutput>>.DataResult(_roleService.SearchRoles(roleNaem));
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        [RbacAuthorize("AdminAndCustomer")]
        public Results<int> Add([FromBody]RoleInput role)
        {
            return Results<int>.DataResult(_roleService.Add(role));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        [HttpDelete]
        [RbacAuthorize("AdminAndCustomer")]
        public Results<int> Delete([FromBody] List<int> idlist)
        {
            return Results<int>.DataResult(_roleService.Delete(idlist.ToArray()));
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        [RbacAuthorize("AdminAndCustomer")]
        public Results<int> Update(RoleInput role)
        {
            return Results<int>.DataResult (_roleService.Update(role));
        }


    }
}
